﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Web;
using System.Net;
using System.IO;
using System.Collections;
using System.Timers;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
namespace BallData
{
    public class Gooooal_NetDataHelper
    {

        public static MatchData matchdataobj = new MatchData();

        public static MainUIControl mainuicontrol = null;

        public static Hashtable requests = Hashtable.Synchronized(new Hashtable());

        
        public static Hashtable oddschgtoshow = new Hashtable();

        public static List<String> failrequest = new List<String>();
        
        /// <summary>
        ///  获取赔率更改通知
        /// </summary>
        /// <returns></returns>
        public static void GetOddsChg()
        {
            StringBuilder strbuilder = new StringBuilder();
            strbuilder.Append("http://odds.gooooal.com/data/upOdds.d?m=");
             
            strbuilder.Append(Convert.ToUInt64(OddsUntil.ConvertDateTimeInt(DateTime.Now) * 1000));

            HttpWebRequest httprequest = (HttpWebRequest)WebRequest.Create(strbuilder.ToString());

            httprequest.ContentType = "text/html; charset=utf-8";

            HttpWebResponse response = null;
            try
            {
                response = (HttpWebResponse)httprequest.GetResponse();
            }
            catch (System.Exception ex)
            {
                Console.Write(ex.Message);
                return;
            }


            try
            {
                StreamReader sr = new StreamReader(response.GetResponseStream());
                // 得到赛事数据
                String matchdata = sr.ReadToEnd();

                if (matchdata.Length == 0 || matchdata == "")
                    return;

                sr.Close();
                response.Close();

                String[] domains = matchdata.Split(OddsUntil.SplitDomain);

                //处理公共数据域
                String[] publicDomain = domains[0].Split(OddsUntil.SplitColumn);
                UInt32 TimeStamp = publicDomain[0] != "" ? Convert.ToUInt32(publicDomain[0], 10) : (UInt32)OddsUntil.ConvertDateTimeInt(DateTime.Now);
                UInt32 OddsChgNum = 0;
                if (publicDomain.Length > 1)
                    OddsChgNum = Convert.ToUInt32(publicDomain[1], 10);

                OddsChg oddsItem = null;

                List<OddsChg> listoddschg = new List<OddsChg>();
                //处理赔率数据域
                String[] oddsDomain = domains[1].Split(OddsUntil.SplitRecord);
                for (int i = 0; i < oddsDomain.Length; i++)
                {
                    if (oddsDomain[i].Length > 5)
                    {
                        oddsItem = new OddsChg(oddsDomain[i]);
                        listoddschg.Add(oddsItem);
                    }
                }
                
 
                foreach (OddsChg oddschg in listoddschg)
                {
                    foreach (CompanyOdds sh in matchdataobj.companymatchsodds.Values)
                    {
                        if (sh.cId == oddschg.cId && sh.o_data != null)
                        {
                                      
                            // 存储即时变化赔率数据到公司的赔率列表里面
                            OddsData oddsdataobj = sh.o_data;

                            Hashtable oddsList = null, mList = null;
                            List<OddsInfo> oddsinfolist = null;
                            bool newchgodds = false;
                            mList = (Hashtable)oddsdataobj.MatchList[oddschg.mId];
                            if (mList != null)
                            {
                                oddsList = (Hashtable)mList[oddschg.sId];
                            }
                            if (oddsList != null)
                            {
                                oddsinfolist = oddsList[oddschg.oType] as List<OddsInfo>;
                            }

                            if (oddsinfolist == null)
                            {
                                if (mList == null)
                                {
                                    mList = new Hashtable();
                                    oddsdataobj.MatchList.Add(oddschg.mId, mList);
                                }

                                if (oddsList == null)
                                {
                                    oddsList = new Hashtable();
                                    mList.Add(oddschg.sId, oddsList);
                                }

                                OddsInfo odds = new OddsInfo(oddschg.mId + "^" + oddschg.sId + "^" + oddschg.oType + "^^^^^^^^^");

                                odds.setChgOdds(oddschg);
                                odds.chgTime = oddschg.chgTime;
                                oddsinfolist = new List<OddsInfo>();

                                oddsinfolist.Add(odds);

                                oddsList.Add(oddschg.oType, oddsinfolist);

                                newchgodds = true;

                            }
                            else
                            {
                                OddsInfo odds = new OddsInfo(oddschg.mId + "^" + oddschg.sId + "^" + oddschg.oType + "^^^^^^^^^");
                                odds.setChgOdds(oddschg);
                                odds.chgTime = oddschg.chgTime;

                                bool bold = false;
                                foreach (OddsInfo i in oddsinfolist)
                                {
                                    if (i.chgTime == odds.chgTime)
                                        bold = true;
                                }
                                if (!bold)
                                {
                                    oddsinfolist.Add(odds);
                                    newchgodds = true;
                                }
                            }

                            if (newchgodds)
                            {
                                StringBuilder key = new StringBuilder();
                                key.Append(oddschg.chgTimeUTC);
                                key.Append(oddschg.mId);
                                key.Append(oddschg.cId);
                                key.Append(oddschg.oType);
                                if (!oddschgtoshow.ContainsKey(key))
                                    oddschgtoshow.Add(key, oddschg);    
                            
                            }



                        }
                    }

                }
                   
            }
            catch (System.Exception ex)
            {
                Console.Write(ex.Message);
            }
            finally
            {
            }

        }

        /// <summary>
        /// 获取彩票竞猜场次数据结束
        /// </summary>
        /// <param name="asyncResult"></param>
        /// <returns></returns>
        public static void GetMathOddsDataForCaiPiaoOk(IAsyncResult asyncResult)
        {
            
            mainuicontrol.OnNetGetDataOk("GetMathOddsDataForCaiPiaoOk");
        }

        /// <summary>
        /// 获得彩票竞猜的比赛场次
        /// </summary>
        /// <returns></returns>
        public static void GetMathOddsDataForCaiPiao()
        {
            StringBuilder strcaipiaobuilder = new StringBuilder();
            strcaipiaobuilder.Append("http://caipiao.gooooal.com/issue/jzspf.lt?");
            strcaipiaobuilder.Append(OddsUntil.GetRandStr(10));


            HttpWebRequest httprequest = (HttpWebRequest)WebRequest.Create(strcaipiaobuilder.ToString());

            httprequest.ContentType = "text/html; charset=utf-8";

            HttpWebResponse response = null;
            String matchdata = null;
            try
            {
                response = (HttpWebResponse)httprequest.GetResponse();

                StreamReader sr = new StreamReader(response.GetResponseStream());
                // 得到赛事数据
                matchdata = sr.ReadToEnd();
                sr.Close();
                response.Close();
            }
            catch (System.Exception ex)
            {
                Console.Write(ex.Message);
                return;
            }


            

            // 分开数据域
            String[] domains = matchdata.Split(OddsUntil.SplitDomain);

            // 分开公共 联盟 赛事 公司 数据
            String[] publicDomain = domains[0].Split(OddsUntil.SplitColumn);


            matchdataobj.TimeStamp = OddsUntil.ConvertIntDateTime(Convert.ToInt32(publicDomain[0]));

            //matchdataobj.LeagueNum = Convert.ToInt32(publicDomain[1], 10);
            //matchdataobj.MatchNum = Convert.ToInt32(publicDomain[2], 10);
            //matchdataobj.CompanyNum = Convert.ToInt32(publicDomain[3], 10);

            String[] IssueInfoDomain = domains[1].Split(OddsUntil.SplitRecord);

            // 竞猜比赛的信息信息,比如开赛时间,结束等
            matchdataobj.Issueinfotable.Clear();
            foreach (String IssueInfo in IssueInfoDomain)
            {
                if (IssueInfo.Length > 5)
                {
                    IssueInfo issueinfo = new IssueInfo(IssueInfo);

                    matchdataobj.Issueinfotable.Add(issueinfo.lotNumber, issueinfo);

                }


            }

            // 竞猜场次的sp,让球等数据
            String[] MatchDomain = domains[2].Split(OddsUntil.SplitRecord);
            //matchdataobj.MatchList.Clear();



            foreach (String Match in MatchDomain)
            {
                if (Match.Length > 5)
                {
                    MathItem_CaiPiao matchitem = new MathItem_CaiPiao(Match);

                    if (!matchdataobj.CaiPiaoMatchList.ContainsKey(matchitem.matchId))
                    {
                        matchdataobj.CaiPiaoMatchList.Add(matchitem.matchId, matchitem);
                    }



                }


            }



        }

        public static void OngetoddsbycompanyOk(IAsyncResult asyncResult)
        {
            if (Gooooal_NetDataHelper.requests.Count > 0)
            {
                int okcount = 0;
                lock (Gooooal_NetDataHelper.requests.SyncRoot)
                {
                    foreach (bool ok in Gooooal_NetDataHelper.requests.Values)
                    {
                        if (ok)
                        {
                            okcount++;
                        }
                    }
                }
                
                if (Gooooal_NetDataHelper.requests.Count == okcount)
                {
                    mainuicontrol.OnNetGetDataOk("OnGetMatOddsDataOk");
                }
            }

        }
        /// <summary>
        /// 获取比赛数据结束调用
        /// </summary>
        /// <param name="asyncResult"></param>
        /// <returns></returns>
        public static void OnGetMatOddsDataOk(IAsyncResult asyncResult)
        {
            
            
            if (Gooooal_NetDataHelper.requests.Count > 0 )
            {
                int okcount = 0;
                foreach (bool ok in Gooooal_NetDataHelper.requests.Values)
                {
                    if (ok)
                    {
                        okcount++;
                    }
                }
                if (Gooooal_NetDataHelper.requests.Count == okcount)
                {
                    

                    DateTime now = DateTime.Now;

                    Hashtable midrequest = new Hashtable();
                    for (int d = 0; d < OddsUntil.GETINDAY; d++)
                    {

                        // 根据公司ID读取公司的赔率数据
                        foreach (Int32 companyid in OddsUntil.defaultCompany)
                        {
                            StringBuilder companyrequest = new StringBuilder();
                            companyrequest.Append("http://odds.gooooal.com/");
                            companyrequest.Append("data/");
                            companyrequest.Append(now.ToString("yyyy"));
                            companyrequest.Append("/");
                            companyrequest.Append(now.ToString("MM"));
                            companyrequest.Append("/");
                            companyrequest.Append(companyid);
                            companyrequest.Append(".");
                            companyrequest.Append(now.ToString("yyyy"));
                            companyrequest.Append("_");
                            companyrequest.Append(now.ToString("MM"));
                            companyrequest.Append("_");
                            companyrequest.Append(now.ToString("dd"));
                            companyrequest.Append("_o");



                            if (midrequest.ContainsKey(companyid))
                            {
                                List<String> requestlist = midrequest[companyid] as List<String>;
                                requestlist.Add(companyrequest.ToString());
                            }
                            else
                            {
                                List<String> requestlist = new List<String>();
                                requestlist.Add(companyrequest.ToString());
                                midrequest.Add(companyid, requestlist);
                            }
                            

                            Gooooal_NetDataHelper.requests.Add(companyrequest.ToString(), false);

                        }
                        now = now.AddDays(1);
                    }

                    foreach (Int32 mid in midrequest.Keys)
                    {
                        List<String> requestlist = midrequest[mid] as List<String>;

                        foreach (String request in requestlist)
                        {
                            
                            Ongetoddsbycompany onget = new Ongetoddsbycompany(getoddsbycompany);
                            onget.BeginInvoke(request, Convert.ToString(mid), OngetoddsbycompanyOk, onget);
                        }
                        
                    
                    }


                }
            }
        }

        public delegate void OnGetMatOddsData(String url);

        public delegate void Ongetoddsbycompany(String url,String cid);

        /// <summary>
        /// 异步调用,获取比赛数据
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static void GetMatOddsData(String url)
        {

            HttpWebRequest httprequest = (HttpWebRequest)WebRequest.Create(url);

            httprequest.Timeout = 30000;
            httprequest.ContentType = "text/html; charset=utf-8";

            HttpWebResponse response = null;
            String matchdata = null;
            try
            {
                response = (HttpWebResponse)httprequest.GetResponse();

                StreamReader sr = new StreamReader(response.GetResponseStream());
                // 得到赛事数据
                matchdata = sr.ReadToEnd();
                sr.Close();
                response.Close();
            }
            catch (System.Exception ex)
            {
                Console.Write(ex.Message);
                return;
            }

            if (matchdata.Length == 0)
                return;
            
            // 分开数据域
            String[] domains = matchdata.Split(OddsUntil.SplitDomain);

            // 分开一些公共数据
            String[] publicDomain = domains[0].Split(OddsUntil.SplitColumn);


            matchdataobj.TimeStamp = OddsUntil.ConvertIntDateTime(Convert.ToInt32(publicDomain[0]));

            //matchdataobj.LeagueNum = Convert.ToInt32(publicDomain[1], 10);
            //matchdataobj.MatchNum = Convert.ToInt32(publicDomain[2], 10);
            //matchdataobj.CompanyNum = Convert.ToInt32(publicDomain[3], 10);
            // 分开联赛联盟,比如英超,意甲等
            String[] LeagueDomain = domains[1].Split(OddsUntil.SplitRecord);
            //matchdataobj.LeagueList.Clear();
            foreach (String League in LeagueDomain)
            {
                if (League.Length > 5)
                {
                    LeagueItem leagueitem = new LeagueItem(League);

                    if (!matchdataobj.LeagueList.ContainsKey(leagueitem.lId))
                    {
                        matchdataobj.LeagueList.Add(leagueitem.lId, leagueitem);
                    }

                }


            }

            // 分开比赛对阵信息包括中英繁名字等数据
            String[] MatchDomain = domains[2].Split(OddsUntil.SplitRecord);
            //matchdataobj.MatchList.Clear();
            foreach (String Match in MatchDomain)
            {
                if (Match.Length > 5)
                {
                    MatchItem matchitem = new MatchItem(Match);

                    if (!matchdataobj.Matchs.ContainsKey(matchitem.mId))
                    {
                        matchdataobj.Matchs.Add(matchitem.mId, matchitem);
                    }


                    //生成列表



                    if (DateTime.Now.Ticks - matchitem.koTimeUTC > 5 * 60)
                    {
                        matchitem.bOver = true;
                    }
                    else
                    {
                        matchdataobj.MatchNum++;
                    }

                    LeagueItem leagueitem = (LeagueItem)matchdataobj.LeagueList[matchitem.lId];


                    if (leagueitem != null)
                    {
                        if (matchitem.bOver)
                        {
                            leagueitem.overNum++;
                        }
                        else
                        {
                            leagueitem.matchNum++;
                        }
                    }
                }


            }

            // 分开博彩公司信息
            // matchdataobj.CompanyList = {id=companyitem}
            String[] CompanyDomain = domains[3].Split(OddsUntil.SplitRecord);
            //matchdataobj.CompanyList.Clear();
            foreach (String company in CompanyDomain)
            {
                if (company.Length > 5)
                {
                    CompanyItem companyitem = new CompanyItem(company);
                    if (companyitem.cId == "201")
                        continue;
                    if (!matchdataobj.CompanyList.ContainsKey(companyitem.cId))
                    {
                        matchdataobj.CompanyList.Add(companyitem.cId, companyitem);
                    }

                    companyitem.cType = OddsUntil.GetCompanyType(companyitem.cId, companyitem.cnName);

                    Int32 nd = 0;
                    if (matchdataobj.CTypeNum.Contains(companyitem.cType))
                    {
                        nd = (Int32)matchdataobj.CTypeNum[companyitem.cType];
                    }

                    nd++;
                    matchdataobj.CTypeNum[companyitem.cType] = nd;

                }


            }

            Gooooal_NetDataHelper.requests[url] = true;

            //////////////////////////////////////////////////////////////////////////

        }

        /// <summary>
        /// 异步调用,更新赔率改变UI
        /// </summary>
        /// <param name="mId"></param>
        /// <param name="oddschg"></param>
        /// <returns></returns>
        public static void UpdateOddsChgUI(String mId, OddsChg oddschg)
        {
            MatchItem mt = matchdataobj.Matchs[mId] as MatchItem;

            if (mt == null)
            {
                Console.WriteLine("no find match " + mId);
                return;
            }

            LeagueItem gt = matchdataobj.LeagueList[mt.lId] as LeagueItem;

            if (gt == null)
            {
                Console.WriteLine("no LeagueItem " + mId);
                return;
            }
            CompanyItem ci = matchdataobj.CompanyList[oddschg.cId] as CompanyItem;

            

            Console.WriteLine("UpdateOddsChgUI:" + mId + "name:" + mt.t1CnName + " vs " + mt.t2CnName + " " + gt.cnName + " " + ci.cnName);
            try
            {

                ObservableCollection<MatchListDataTemple> matchlist = OddsUntil.LeagueViewTemple[gt.lId] as ObservableCollection<MatchListDataTemple>;

                List<MatchListDataTemple> updatematchlist = new List<MatchListDataTemple>();

                foreach (MatchListDataTemple mdt in matchlist)
                {
                    if (mdt.mId == mId)
                    {
                        if (mdt.CompanyName != ci.cnName)
                        {
                            //LogList.Add("更新后台数据 不同公司");
                            MatchItem matchitem = matchdataobj.Matchs[mt.mId] as MatchItem;
                            CompanyOdds sh = matchdataobj.companymatchsodds[oddschg.cId] as CompanyOdds;
                            LeagueItem leagueitem = matchdataobj.LeagueList[mt.lId] as LeagueItem;
                            OddsInfo odds_asia = null, odds_eupro = null, odds_bigsmall = null;

                            if (sh == null)
                            {
                                mainuicontrol.getLogBoxList().Add("从服务器获取:" + ci.cnName);

                                Gooooal_NetDataHelper.getoddsbycompanyInday(ci.cId);

                                sh = mainuicontrol.getMatchData().companymatchsodds[ci.cId] as CompanyOdds;
                            }

                            if (sh != null)
                            {
                                Hashtable mList = sh.o_data.MatchList[matchitem.mId] as Hashtable;

                                OddsUntil.GetDataBymList(ref odds_asia, ref odds_eupro, ref odds_bigsmall, ref mList);
                            }

                            MatchListDataTemple matchlistdatatemple = new MatchListDataTemple
                            {
                                mId = matchitem.mId,
                                LeagueName = leagueitem.cnName,
                                LeagueColor = leagueitem.color,
                                MatchHost = matchitem.t1CnName,
                                MatchVisit = matchitem.t2CnName,
                                CompanyName = ci.cnName,
                                CompanyColor = OddsUntil.companyLight,
                                Odds_a_host = odds_asia.host,
                                Odds_a_vs = OddsUntil.GetVsName(odds_asia.vs, "tr"),
                                Odds_a_visit = odds_asia.visit,

                                Odds_e_host = odds_eupro.host,
                                Odds_e_vs = odds_eupro.vs,
                                Odds_e_visit = odds_eupro.visit,

                                Odds_o_host = odds_bigsmall.host,
                                Odds_o_vs = OddsUntil.GetTotalName(odds_bigsmall.vs),
                                Odds_o_visit = odds_bigsmall.visit,
                                MatchKoTime = matchitem.koDay,

                                Odds_a_host_color = OddsUntil.White,
                                Odds_a_visit_color = OddsUntil.White,

                                Odds_o_host_color = OddsUntil.White,
                                Odds_o_visit_color = OddsUntil.White,

                                Odds_e_host_color = OddsUntil.White,
                                Odds_e_visit_color = OddsUntil.White,

                                Odds_a_chgtime = odds_asia.chgTime,
                                Odds_e_chgtime = odds_eupro.chgTime,
                                Odds_o_chgtime = odds_bigsmall.chgTime,

                            };

                            OddsUntil.MathViewTemple[OddsUntil.GetMathViewKey(mdt.mId, sh.cId)] = matchlistdatatemple;

                            updatematchlist.Add(matchlistdatatemple);
                            

                            
                        }
                        else
                        {

                            if (mdt.Odds_a_chgtime != oddschg.chgTime)
                            {
                                if (oddschg.oType == "a")
                                {
                                    mdt.Odds_a_host_color = mdt.Odds_a_host != oddschg.host ? OddsUntil.host_a : OddsUntil.White;
                                    mdt.Odds_a_visit_color = mdt.Odds_a_visit != oddschg.visit ? OddsUntil.visit_a : OddsUntil.White;
                                    mdt.Odds_a_vs = OddsUntil.GetVsName(oddschg.vs, "tr");
                                    mainuicontrol.getLogBoxList().Add("UpdateOddsChgUI:" + mId + "name:" + mt.t1CnName + " vs " + mt.t2CnName + " " + gt.cnName + " " + ci.cnName);
                                    mainuicontrol.getLogBoxList().Add("更新亚洲盘");
                                }
                                if (oddschg.oType == "o")
                                {
                                    mdt.Odds_o_host_color = mdt.Odds_o_host != oddschg.host ? OddsUntil.host_o : OddsUntil.White;
                                    mdt.Odds_o_visit_color = mdt.Odds_o_visit != oddschg.visit ? OddsUntil.visit_o : OddsUntil.White;
                                    mdt.Odds_o_vs = OddsUntil.GetTotalName(oddschg.vs);
                                    mainuicontrol.getLogBoxList().Add("UpdateOddsChgUI:" + mId + "name:" + mt.t1CnName + " vs " + mt.t2CnName + " " + gt.cnName + " " + ci.cnName);
                                    mainuicontrol.getLogBoxList().Add("更新大小盘");
                                }
                                if (oddschg.oType == "e")
                                {
                                    mdt.Odds_e_host_color = mdt.Odds_e_host != oddschg.host ? OddsUntil.host_e : OddsUntil.White;
                                    mdt.Odds_e_visit_color = mdt.Odds_e_visit != oddschg.visit ? OddsUntil.visit_e : OddsUntil.White;
                                    mainuicontrol.getLogBoxList().Add("UpdateOddsChgUI:" + mId + "name:" + mt.t1CnName + " vs " + mt.t2CnName + " " + gt.cnName + " " + ci.cnName);
                                    mdt.Odds_e_vs = oddschg.vs;
                                    mainuicontrol.getLogBoxList().Add("更新欧洲盘");
                                }


                                mdt.Odds_a_host = oddschg.host;
                                mdt.Odds_a_visit = oddschg.visit;


                                mdt.Odds_a_chgtime = oddschg.chgTime;



                                //bUpdate = true;

                                foreach (TabItem tb in mainuicontrol.getLeagueTab().Items)
                                {
                                    if (tb.Header as String == gt.cnName)
                                    {
                                        tb.Background = new SolidColorBrush(Colors.Lime);
                                    }
                                    else if (tb.Header as String == "其他")
                                    {
                                        foreach (MenuItem mi in tb.ContextMenu.Items)
                                        {
                                            if (mi.Header as String == gt.cnName)
                                            {
                                                mi.Background = new SolidColorBrush(Colors.Lime);
                                                tb.Background = new SolidColorBrush(Colors.Lime);
                                            }
                                        }
                                    }
                                }


                            }


                        }
                    }

                }

                
                foreach (MatchListDataTemple updatemtp in updatematchlist)
                {
                    MatchListDataTemple find = null;
                    foreach(MatchListDataTemple mtp in matchlist)
                    {
                        if (mtp.mId == updatemtp.mId)
                        {
                            find = mtp;
                            break;
                        }
                    }

                    int index = matchlist.IndexOf(find);
                    matchlist.Remove(find);
                    matchlist.Insert(index, updatemtp);

                    foreach (TabItem tb in mainuicontrol.getLeagueTab().Items)
                    {
                        if (tb.Header as String == gt.cnName)
                        {
                            tb.Background = new SolidColorBrush(Colors.Lime);

                            ListView listview = tb.Content as ListView;


                            if (matchlist != null && matchlist.Count > 0)
                            {
                                var sortlist = matchlist.OrderBy(p => p.lMatchKoTime);
                                matchlist = new ObservableCollection<MatchListDataTemple>(sortlist);

                                listview.ItemsSource = matchlist;


                            }
                        }
                        else if (tb.Header as String == "其他")
                        {
                            foreach (MenuItem mi in tb.ContextMenu.Items)
                            {
                                if (mi.Header as String == gt.cnName)
                                {
                                    mi.Background = new SolidColorBrush(Colors.Lime);
                                    tb.Background = new SolidColorBrush(Colors.Lime);
                                }
                            }
                        }
                    }
                }
                


            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {

            }

            //Console.WriteLine("UpdateOddsChgUI end....");




        }

        /// <summary>
        /// 获取比赛赔率结束
        /// </summary>
        /// <param name="asyncResult"></param>
        /// <returns></returns>
        public static void OnGetMatOddsChgDataOk(IAsyncResult asyncResult)
        {
             mainuicontrol.GetDispatcher().BeginInvoke((Action)(() =>
            {
                mainuicontrol.StartTimeTick();
                if(oddschgtoshow.Count > 0)
                    mainuicontrol.getOddsChangePanel().Children.Clear();
                Int32 lasttime = 0;
                foreach (OddsChg oddschg in oddschgtoshow.Values)
                {
                    MatchItem mt = matchdataobj.Matchs[oddschg.mId] as MatchItem;

                    if (mt == null)
                    {
                        Console.WriteLine("no find match " + oddschg.mId);
                        continue;
                    }

                    LeagueItem gt = matchdataobj.LeagueList[mt.lId] as LeagueItem;

                    if (gt == null)
                    {
                        Console.WriteLine("no LeagueItem " + oddschg.mId);
                        continue;
                    }

                    if(oddschg.chgTimeUTC > lasttime)
                        lasttime = oddschg.chgTimeUTC;
                    CompanyItem ci = matchdataobj.CompanyList[oddschg.cId] as CompanyItem;

                    OddsChangeItem oitem = new OddsChangeItem();

                    OddsChangeItemBind oitembind = new OddsChangeItemBind { LeagueName = gt.cnName + (oddschg.oType == "a" ? "[亚洲盘]":oddschg.oType == "a"?"[欧洲盘]":"[大小盘]"), HostTeamName = mt.t1CnName, HostOdds = oddschg.host, Panlu = oddschg.oType == "a" ? OddsUntil.GetVsName(oddschg.vs, "tr") : oddschg.oType == "o" ? OddsUntil.GetTotalName(oddschg.vs) : oddschg.vs, VisitTeanName = mt.t2CnName, VisitOdds = oddschg.visit };

                    oitem.Oddschangeitembind = oitembind;

                    mainuicontrol.getOddsChangePanel().Children.Insert(0, oitem);

                    UpdateOddsChgUI(oddschg.mId, oddschg);

                }
                oddschgtoshow.Clear();
                mainuicontrol.getLogBoxList().Add("获取赔率改变数据OK!!");
                if (lasttime > 0)
                {
                    String lastoddsinfo = "最新盘口变动 [" + OddsUntil.ConvertIntDateTime(lasttime).ToString() + "]";
                    mainuicontrol.UpdateLastOddsInfo(lastoddsinfo);
                }
                
            }));

           
           

        }
        
        /// <summary>
        /// 异步根据公司ID 获取赔率信息
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public static void getoddsbycompany(String url,String cid)
        {
           
            String oddsdata = null;
            try
            {
                HttpWebRequest httprequest = (HttpWebRequest)WebRequest.Create(url);

                httprequest.ContentType = "text/html; charset=utf-8";

                HttpWebResponse response = (HttpWebResponse)httprequest.GetResponse();

                StreamReader sr = new StreamReader(response.GetResponseStream());
                // 得到赔率数据
                oddsdata = sr.ReadToEnd();
                sr.Close();
                response.Close();
            }
            catch (System.Exception ex)
            {
               
                Console.WriteLine(ex.Message);
                return;
            }
            finally
            {
                Gooooal_NetDataHelper.requests[url] = true;
            }

            if (oddsdata.Length == 0)
                return;

            OddsData oddsdataobj = null;

            CompanyOdds sh = null;

            lock (matchdataobj.companymatchsodds.SyncRoot)
            {
                if (matchdataobj.companymatchsodds.Contains(cid))
                {
                    sh = (CompanyOdds)matchdataobj.companymatchsodds[cid];
                    oddsdataobj = sh.o_data;

                    //Console.WriteLine("cid:" + cid);
                }
                else
                {
                    sh = new CompanyOdds();
                    oddsdataobj = new OddsData();
                    sh.o_data = oddsdataobj;
                    sh.cId = cid;

                    matchdataobj.companymatchsodds.Add(cid, sh);


                    //Console.WriteLine("new cid:" + cid);
                }
            }
           

            
            // 分离公共域
            String[] domains = oddsdata.Split(OddsUntil.SplitDomain);
            // 分离赔率数据
            String[] publicDomain = domains[0].Split(OddsUntil.SplitColumn);
            // 更新时间
            oddsdataobj.TimeStamp = Convert.ToInt32(publicDomain[0], 10);
            //oddsdataobj.OddsNum = Convert.ToInt32(publicDomain[1], 10);
            oddsdataobj.OddsNum = 0;

            //var oddsItem, mList, oddsList;

            //处理该公司的赔率数据域
            // company={oddsobj={matchid={companyid=oddslist,companyid2=oddslist2}}}
            String[] oddsDomain = domains[1].Split(OddsUntil.SplitRecord);
            for (int i = 0; i < oddsDomain.Length; i++)
            {
                if (oddsDomain[i].Length > 5)
                {
                    // 单个赔率数据 包含公司等信息
                    OddsInfo oddsItem = new OddsInfo(oddsDomain[i]);
                    
                    Hashtable mList = null;
                    if (oddsdataobj.MatchList.Contains(oddsItem.mId))
                    {
                        mList = (Hashtable)oddsdataobj.MatchList[oddsItem.mId];
                    }
                    else
                    {
                        mList = new Hashtable();
                        oddsdataobj.MatchList.Add(oddsItem.mId, mList);
                    }

                    // 盘口序号映射,比如 1=[亚盘1,欧盘1,大小1],2=[亚盘2,欧盘2,大小2]
                    Hashtable oddsList = null;

                    if (mList.Contains(oddsItem.sId))
                    {
                        oddsList = (Hashtable)mList[oddsItem.sId];
                    }
                    else
                    {
                        oddsList = new Hashtable();
                        mList.Add(oddsItem.sId, oddsList);

                    }
                    if (oddsList.ContainsKey(oddsItem.oType))
                    {
                        List<OddsInfo> oddsinfolist = oddsList[oddsItem.oType] as List<OddsInfo>;
                        oddsinfolist.Add(oddsItem);
                        
                    }
                    else
                    {
                        List<OddsInfo> oddsinfolist = new List<OddsInfo>();
                        oddsinfolist.Add(oddsItem);
                        oddsList.Add(oddsItem.oType, oddsinfolist);
                    }


                    
                    

                }
            }


            lock(Gooooal_NetDataHelper.requests.SyncRoot)
            {
                Gooooal_NetDataHelper.requests[url] = true;
            }
            
            
            
        }

        /// <summary>
        /// 同步获取公司赔率数据
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public static void getoddsbycompanyInday(String cid)
        {
            DateTime now = DateTime.Now;
            for (int d = 0; d < OddsUntil.GETINDAY; d++)
            {
                StringBuilder companyrequest = new StringBuilder();
                companyrequest.Append("http://odds.gooooal.com/");
                companyrequest.Append("data/");
                companyrequest.Append(now.ToString("yyyy"));
                companyrequest.Append("/");
                companyrequest.Append(now.ToString("MM"));
                companyrequest.Append("/");
                companyrequest.Append(cid);
                companyrequest.Append(".");
                companyrequest.Append(now.ToString("yyyy"));
                companyrequest.Append("_");
                companyrequest.Append(now.ToString("MM"));
                companyrequest.Append("_");
                companyrequest.Append(now.ToString("dd"));
                companyrequest.Append("_o");

                Gooooal_NetDataHelper.getoddsbycompany(companyrequest.ToString(), cid);

                now = now.AddDays(1);
            }
        }
        /// <summary>
        /// 请求网站数据初始化
        /// </summary>
        /// <param name="ui"></param>
        /// <returns></returns>
        public static void Init(MainUIControl ui)
        {
            mainuicontrol = ui;
            // 请求获取当前日期的赛事信息
            DateTime now = DateTime.Now;
            
            List<String> requestlist = new List<String>();
            for (int i = 0; i < OddsUntil.GETINDAY; i++)
            {
                StringBuilder strbuilder = new StringBuilder();
                strbuilder.Append("http://odds.gooooal.com/data/");
                strbuilder.Append(now.ToString("yyyy"));
                strbuilder.Append("/");
                strbuilder.Append(now.ToString("MM"));
                strbuilder.Append("/");
                strbuilder.Append("matchdata.");
                strbuilder.Append(now.ToString("yyyy"));
                strbuilder.Append("_");
                strbuilder.Append(now.ToString("MM"));
                strbuilder.Append("_");
                strbuilder.Append(now.ToString("dd"));
                strbuilder.Append("_");
                strbuilder.Append("d?");
                strbuilder.Append(OddsUntil.GetRandStr(10));

                requests.Add(strbuilder.ToString(), false);

                requestlist.Add(strbuilder.ToString());
                
                now = now.AddDays(1);
            }

            foreach (String request in requestlist)
            {
                OnGetMatOddsData onget = new OnGetMatOddsData(GetMatOddsData);
                onget.BeginInvoke(request, OnGetMatOddsDataOk, onget);
            }


            // 开赛获得足彩竞猜比赛
            Action action3 = GetMathOddsDataForCaiPiao;
            action3.BeginInvoke(GetMathOddsDataForCaiPiaoOk, action3);


        
        }
    }
}
